<?php

function get_observation_page($observation) {
  if (!is_object($observation)) {
    return NULL;
  }

  $topology = $observation->topology;
  $js = <<<EOT
  <script language="javascript">
    function onMapInitComplete() {
      mapportal.addFeatureFromWKT("{$topology}");
    }

    jQuery(document).ready(function(){
      if (typeof mapportal.onMapInitComplete == "boolean") {
        onMapInitComplete();
      } else {
        mapportal.onMapInitComplete = onMapInitComplete;
      }
    });
  </script>
EOT;

  $html = '';

  $html .= '<div id="divShowObservation">';
  $html .= contributor_info_html($observation->created_by, $observation->created_date);
  $html .= common_names_fieldset_html($observation->common_names);
  $html .= scientific_info_fieldset_html($observation);
  $html .= ecosystem_fieldset_html($observation);
  $html .= observation_date_html($observation);
  $html .= observation_time_html($observation);
  $html .= location_parameters_fieldset_html($observation->location);
  $html .= waterbody_fieldset_html($observation);
  $html .= no_of_indiv_html($observation->num_individuals);
  $html .= basis_of_record_html($observation->basis_of_record);
  $html .= observation_record_language_html($observation->record_language);
  $html .= observer_details_fieldset_html($observation->observer);
  $html .= comments_html($observation->comments);
  $html .= validation_html($observation->validation);
  $html .= '</div>';

  $html .= get_observation_show_map_html();
  $html .= get_preview_images_html($observation->id);

  return $js . $html;
}

function get_observation_show_map_html() {
  $zoomtext = t('Zoom');

  $html = <<<EOT
  <div id="mapArea">
    <div id="divMPAddress">
      <input type="text" id="txtMPAddress"></input><input type="button" id="btnMPGotoAddr" value="{$zoomtext}"></input>
    </div>
    <!-- map -->
    <div id="map" style="height: 100%; width: 100%;">

    </div>
    <!-- map ends -->
  </div>
  <!-- mapArea ends -->
EOT;

  return $html;
}

function contributor_info_html($created_by, $created_date) {
  return '<div class="info-page">Contributed by ' . get_user_name($created_by) . '</div>';
}

function common_names_fieldset_html($common_names) {
  $html = '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Common Names') . '</legend>';
  $i = 0;
  foreach ($common_names as $common_name) {
    $html .= common_name_fieldset_html($common_name, $i);
    $i++;
  }
  $html .= '    </fieldset>';

  return $html;
}

function common_name_fieldset_html($common_name, $i) {
  if ($i == 0) {
    $i = '';
  }
  $html = '      <fieldset class=" collapsible">';
  $html .= '        <legend>' .  t('Common Name ' . $i) . '</legend>';
  $html .= '        <div class="form-item" id="edit-wo-common-name-wrapper">';
  $html .= '          <label class="same-line">';
  $html .= '            ' .  t('Common Name') . ': ';
  $html .= '          </label>';
  $html .= '          ' . t($common_name['common_name']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-common-name-language-wrapper">';
  $html .= '          <label for="edit-wo-common-name-language" class="same-line">' .  t('Language of Common Name') . ': </label>';
  $html .= '          ' . t($common_name['cn_language']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-common-name-region-wrapper">';
  $html .= '          <label for="edit-wo-common-name-region" class="same-line">' .  t('Region') . ': </label>';
  $html .= '          ' . t($common_name['region']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-common-name-gender-wrapper">';
  $html .= '          <label for="edit-wo-common-name-gender" class="same-line">' .  t('Gender') . ': </label>';
  $html .= '          ' . t($common_name['gender']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-common-name-lifestage-wrapper">';
  $html .= '          <label for="edit-wo-common-name-lifestage" class="same-line">' .  t('Lifestage') . ': </label>';
  $html .= '          ' . t($common_name['lifestage']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  if (strtolower($common_name['cn_language']) != 'english') {
    $html .= '        <div class="form-item" id="edit-wo-common-name-english-wrapper">';
    $html .= '          <label class="same-line">';
    $html .= '            ' .  t('English Spelling of Common Name') . ': ';
    $html .= '          </label>';
    $html .= '          ' . t($common_name['common_name_english']);
    $html .= '          <div class="description"></div>';
    $html .= '        </div>';
  }
  $html .= '      </fieldset>';

  return $html;
}

function scientific_info_fieldset_html($observation) {
  $html = '';

  $html .= '    <fieldset class=" collapsible"><legend>' .  t('Scientific Information') . '</legend>';
  $html .= '      <fieldset class=" collapsible"><legend>' .  t('Scientific Name') . '</legend>';
  $html .= '        <div class="form-item" id="edit-wo-scientific-name-wrapper">';
  $html .= '          <label for="edit-wo-scientific-name" class="same-line">' .  t('Scientific Name') . '</label>';
  $html .= '          ' . t($observation->scientific_name_info['s_name']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-scientific-name-author-wrapper">';
  $html .= '          <label for="edit-wo-scientific-name-author" class="same-line">' .  t('Author') . ': </label>';
  $html .= '          ' . t($observation->scientific_name_info['s_author']);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '';
  $html .= '        <div class="form-item" id="edit-wo-scientific-name-year-wrapper">';
  $html .= '          <label for="edit-wo-scientific-name-year" class="same-line">' .  t('Year') . ': </label>';
  $yr = $observation->scientific_name_info['s_year'];
  if ((int)$yr == 0) {
    $yr = '';
  }
  $html .= '          ' . t($yr);
  $html .= '          <div class="description"></div>';
  $html .= '        </div>';
  $html .= '      </fieldset>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-kingdom-wrapper">';
  $html .= '        <label for="edit-wo-kingdom" class="same-line">' .  t('Kingdom: ') . '</label>';
  $html .= '        ' . t($observation->s_kingdom);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-phylum-wrapper">';
  $html .= '        <label for="edit-wo-phylum" class="same-line">' .  t('Phylum') . ': </label>';
  $html .= '        ' . t($observation->s_phylum);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-class-wrapper">';
  $html .= '        <label for="edit-wo-class" class="same-line">' .  t('Class') . ': </label>';
  $html .= '        ' . t($observation->s_class);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-order-wrapper">';
  $html .= '        <label for="edit-wo-order" class="same-line">' .  t('Order') . ': </label>';
  $html .= '        ' . t($observation->s_order);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-family-wrapper">';
  $html .= '        <label for="edit-wo-family" class="same-line">' .  t('Family') . ': </label>';
  $html .= '        ' . t($observation->s_family);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-genus-wrapper">';
  $html .= '        <label for="edit-wo-genus" class="same-line">' .  t('Genus') . ': </label>';
  $html .= '        ' . t($observation->s_genus);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-species-wrapper">';
  $html .= '        <label for="edit-wo-species" class="same-line">' .  t('Species') . ': </label>';
  $html .= '        ' . t($observation->s_species);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-taxanomic-status-wrapper">';
  $html .= '        <label for="edit-wo-taxanomic-status" class="same-line">' .  t('Taxanomic Status') . ': </label>';
  $html .= '        ' . t($observation->taxanomic_status);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-nomenclature-type-status-wrapper">';
  $html .= '        <label for="edit-wo-nomenclature-type-status" class="same-line">' .  t('Nomenclature Type Status') . ': </label>';
  $html .= '        ' . t($observation->nomenclature_type_status);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '    </fieldset>';

  return $html;
}

function observation_record_language_html($language) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-record-language-wrapper">';
  $html .= '      <label for="edit-wo-record-language" class="same-line">' .  t('Language of Record') . ': </label>';
  $html .= '      ' . t($language);
  $html .= '      <div class="description"></div>';
  $html .= '    </div>';

  return $html;
}

function specimen_gender_html($gender) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-gender-wrapper">';
  $html .= '      <label for="edit-wo-gender" class="same-line">' .  t('Gender') . ': </label>';
  $html .= '      ' . t($gender);
  $html .= '      <div class="description"></div>';
  $html .= '    </div>';

  return $html;
}

function specimen_lifestage_html($lifestage) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-lifestage-wrapper">';
  $html .= '      <label for="edit-wo-lifestage" class="same-line">' .  t('Life Stage') . ': </label>';
  $html .= '      ' . t($lifestage);
  $html .= '      <div class="description"></div>';
  $html .= '    </div>';

  return $html;
}

function no_of_indiv_html($count) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-num-individuals-wrapper">';
  $html .= '      <label for="edit-wo-num-individuals" class="same-line">' .  t('Number of Individuals') . ': </label>';
  $html .= '      ' . t($count);
  $html .= '      <div class="description"></div>';
  $html .= '    </div>';

  return $html;
}

function basis_of_record_html($bor) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-basis-of-record-wrapper">';
  $html .= '      <label for="edit-wo-basis-of-record" class="same-line">' .  t('Basis of Record') . ': </label>';
  $html .= '      ' . t($bor);
  $html .= '      <div class="description"></div>';
  $html .= '    </div>';

  return $html;
}

function observation_date_html($observation) {
  $html = '';

  $html .= '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Observation Date') . '</legend>';

  if ($observation->is_date_range) {
    $html .= '      <div class="form-item" id="edit-wo-date-range-wrapper">';
    $html .= '        <div class="form-item" id="edit-wo-from-date-wrapper">';
    $html .= '          <label for="edit-wo-from-date" class="same-line">' .  t('From Date') . ': </label>';
    $html .= '          ' . t($observation->from_date);
    $html .= '          <div class="description"></div>';
    $html .= '        </div>';
    $html .= '';
    $html .= '        <div class="form-item" id="edit-wo-to-date-wrapper">';
    $html .= '          <label for="edit-wo-to-date" class="same-line">' .  t('To Date') . ': </label>';
    $html .= '          ' . t($observation->to_date);
    $html .= '          <div class="description"></div>';
    $html .= '        </div>';
    $html .= '      </div>';
  }
  else {
    $html .= '      <div class="form-item" id="edit-wo-on-date-wrapper">';
    $html .= '        <label for="edit-wo-on-date" class="same-line">' .  t('Date') . ': </label>';
    $html .= '        ' . t($observation->on_date);
    $html .= '        <div class="description"></div>';
    $html .= '      </div>';
  }

  $html .= '    </fieldset>';

  return $html;
}

function observation_time_html($observation) {
  $html = '';

  $html .= '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Observation Time') . '</legend>';
  if ($observation->is_exact_time) {
    $html .= '      <div class="form-item" id="edit-wo-exact-time-wrapper">';
    $html .= '        <label for="edit-wo-exact-time" class="same-line">' .  t('Exact Time') . ': </label>';
    $html .= '        ' . t($observation->at_time);
    $html .= '        <div class="description"></div>';
    $html .= '      </div>';
  }
  else {
    $html .= '      <div class="form-item" id="edit-wo-part-of-day-wrapper">';
    $html .= '        <label for="edit-wo-part-of-day">' .  t('Part of Day') . ': </label>';
    $html .= '        <ul>';
    foreach ($observation->pods as $pod) {
      $html .= '          <li>' . t($pod);
    }
    $html .= '        </ul>';
    $html .= '        <div class="description"></div>';
    $html .= '      </div>';
  }
  $html .= '    </fieldset>';

  return $html;
}

function positional_parameters_fieldset_html($observation) {
  $html = '';

  $html .= '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Positional Level Parameters') . '</legend>';
  $html .= '      <div class="form-item" id="edit-wo-altitude-wrapper">';
  $html .= '        <label for="edit-wo-altitude" class="same-line">' .  t('Altitude') . ': </label>';
  $html .= '        ' . t($observation->altitude);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-depth-wrapper">';
  $html .= '        <label for="edit-wo-depth" class="same-line">' .  t('Depth') . ': </label>';
  $html .= '        ' . t($observation->depth);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '    </fieldset>';

  return $html;
}

function location_parameters_fieldset_html($location) {
  $html = '';

  $html .= '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Location Information') . '</legend>';
  $html .= '      <div class="form-item" id="edit-wo-country-wrapper">';
  $html .= '        <label for="edit-wo-country" class="same-line">' .  t('Country') . ': </label>';
  $html .= '        ' . t($location['country']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '      ';
  $html .= '      <div class="form-item" id="edit-wo-region-wrapper">';
  $html .= '        <label for="edit-wo-region" class="same-line">' .  t('Region/ State') . ': </label>';
  $html .= '        ' . t($location['region']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '      ';
  $html .= '      <div class="form-item" id="edit-wo-city-wrapper">';
  $html .= '        <label for="edit-wo-city" class="same-line">' .  t('City') . ': </label>';
  $html .= '        ' . t($location['city']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '      ';
  $html .= '      <div class="form-item" id="edit-wo-locality-wrapper">';
  $html .= '        <label for="edit-wo-locality" class="same-line">' .  t('Locality') . ': </label>';
  $html .= '        ' . t($location['locality']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '    </fieldset>';

  return $html;
}

function waterbody_fieldset_html($observation) {
  $html = '';

  if ($observation->is_waterbody_related) {
    $html .= '    <fieldset class=" collapsible">';
    $html .= '      <legend>' .  t('Waterbody Information') . '</legend>';
    $html .= '      <div class="form-item" id="edit-wo-waterbody-type-wrapper">';
    $html .= '        <label for="edit-wo-waterbody-type" class="same-line">' .  t('Waterbody Type') . ': </label>';
    $html .= '        ' . t($observation->waterbody_type);
    $html .= '        <div class="description"></div>';
    $html .= '      </div>';
    $html .= '';
    $html .= '      <div class="form-item" id="edit-wo-waterbody-name-wrapper">';
    $html .= '        <label for="edit-wo-waterbody-name" class="same-line">';
    $html .= '          ' . t('Waterbody Name') . ': ';
    $html .= '        </label>';
    $html .= '        ' . t($observation->waterbody_name);
    $html .= '        <div class="description"></div>';
    $html .= '      </div>';
    $html .= '    </fieldset>';
  }

  return $html;
}

function observer_details_fieldset_html($observer_details) {
  $html = '';

  $html .= '    <fieldset class=" collapsible">';
  $html .= '      <legend>' .  t('Observer Information') . '</legend>';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-first-name-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-first-name" class="same-line">' .  t('First Name') . ': </label>';
  $html .= '        ' . t($observer_details['fname']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-last-name-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-last-name" class="same-line">' .  t('Last Name') . ': </label>';
  $html .= '        ' . t($observer_details['lname']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-email-id-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-email-id" class="same-line">' .  t('Email ID') . ': </label>';
  $html .= '        ' . t($observer_details['email']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-affiliation-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-affiliation" class="same-line">' .  t('Affiliation') . ': </label>';
  $html .= '        ' . t($observer_details['affiliation']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-address1-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-address1" class="same-line">' .  t('Address Line 1') . ': </label>';
  $html .= '        ' . t($observer_details['address1']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-address2-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-address2" class="same-line">' .  t('Address Line 2') . ': </label>';
  $html .= '        ' . t($observer_details['address2']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-city-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-city" class="same-line">' .  t('City') . ': </label>';
  $html .= '        ' . t($observer_details['city']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-region-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-region" class="same-line">' .  t('Region/ State') . ': </label>';
  $html .= '        ' . t($observer_details['region']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-country-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-country" class="same-line">' .  t('Country') . ': </label>';
  $html .= '        ' . t($observer_details['country']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-phone-number-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-phone-number" class="same-line">' .  t('Phone number') . ': </label>';
  $html .= '        ' . t($observer_details['phoneno']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '';
  $html .= '      <div class="form-item" id="edit-wo-other-observer-mobile-wrapper">';
  $html .= '        <label for="edit-wo-other-observer-mobile" class="same-line">' .  t('Mobile Number') . ': </label>';
  $html .= '        ' . t($observer_details['mobile']);
  $html .= '        <div class="description"></div>';
  $html .= '      </div>';
  $html .= '    </fieldset>';

  return $html;
}

function comments_html($comments) {
  $html = '';

  $html .= '    <div class="form-item" id="edit-wo-comments-wrapper">';
  $html .= '      <label for="edit-wo-comments">' .  t('Comments') . ': </label>';
  $html .= '      ' . t($comments);
  $html .= '    </div>';

  return $html;
}

function validation_html($validation) {
  $html = '';

  $html .= '<fieldset>';
  $html .= ' <legend>Validation</legend>';

  $len = count($validation);
  if ($len == 0) {
    $html .= '<div class="error">This record has not yet been validated.</div>';
  }
  else {
    for ($i = 0; $i < $len; $i++) {
      $html .= get_div_for_validation_status($validation[$i]);
    }
  }

  $html .= '</fieldset>';

  return $html;
}

function get_div_for_validation_status($validation_info) {
  $status = $validation_info['status'];
  $colors = array('Pending' => 'warning', 'Approved' => 'status', 'Rejected' => 'error');
  $html = '<div class="messages ' . $colors[$status] . '">';
  $html .= '<div>Status: ' . $status . '</div>';
  $html .= '<div>Comments: ' . $validation_info['comments'] . '</div>';
  $html .= '<div class="info-page">Validated by ' . $validation_info['validated_by'] . ' on ' . $validation_info['validated_date'] . '</div>';
  $html .= '</div>';

  return $html;
}

function ecosystem_fieldset_html($observation) {
  $id = $observation->ecosystem_id;
  $parent_ecosystem = '';
  $child_ecosystem = '';
  $eco = '';
  $parent_id = 0;
  $query = 'select ecosystem_type, parent_id from win_ecosystems where id = %d';
  $query_args = array($id);
  $result = db_query($query, $query_args);
  if (!$result) {
    return FALSE;
  }
  else {
    if ($obj = db_fetch_object($result)) {
      $eco = $obj->ecosystem_type;
      $parent_id = $obj->parent_id;
    }
  }

  if ($parent_id == NULL) {
    $parent_ecosystem = $eco;
  }
  else {
    $child_ecosystem = $eco;

    $query = 'select ecosystem_type from win_ecosystems where id = %d';
    $query_args = array($parent_id);
    $result = db_query($query, $query_args);
    if (!$result) {
      return FALSE;
    }
    else {
      if ($obj = db_fetch_object($result)) {
        $parent_ecosystem = $obj->ecosystem_type;
      }
    }
  }

  $nativeness = $observation->nativeness;
  if ($nativeness == '') {
    $nativeness = '&nbsp;';
  }
  $iucn_threat_category = $observation->iucn_threat_category;
  if ($iucn_threat_category == '') {
    $iucn_threat_category = '&nbsp;';
  }

  $html = <<<EOT
  <fieldset>
    <legend>Ecosystem/ Habitat type</legend>
    <div><label class="same-line" >Type: </label>
      {$parent_ecosystem}
EOT;
  if ($child_ecosystem != '') {
    $html .= ' >> ' . $child_ecosystem;
  }
  $html .= <<<EOT
    </div>
    <div class="form-item">
      <label class="same-line" >Nativeness: </label>
      {$nativeness}
    </div>
    <div class="form-item">
      <label class="same-line" >IUCN Threat Category: </label>
      {$iucn_threat_category}
    </div>
  </fieldset>
EOT;

  return $html;

}

